# -*- coding: utf-8 -*-
# @Time    : 2024/1/23 21:35
# @Author  : micah
# @File    : 4.asyncio集合aiohttp完成数据提取.py
# @Software: PyCharm

import asyncio
import aiohttp
from bs4 import BeautifulSoup

url = 'https://movie.douban.com/top250?start={}&filter='
headers = {
    "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) "
                  "AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36"
}


async def get_movie_info(page):
    async with aiohttp.ClientSession() as session:
        async with session.get(url.format(page * 25), headers=headers) as response:
            soup = BeautifulSoup(await response.text(), 'lxml')
            div_list = soup.find_all('div', class_='hd')
            for title in div_list:
                print(title.text)


async def main():
    tasks = [asyncio.create_task(get_movie_info(page)) for page in range(10)]
    await asyncio.wait(tasks)


if __name__ == '__main__':
    # loop = asyncio.get_event_loop()
    # tasks = [loop.create_task(get_movie_info(page)) for page in range(10)]
    # loop.run_until_complete(asyncio.gather(*tasks))

    # 没有事件循环对象无法直接使用asyncio.create_task创建task对象
    # tasks = [asyncio.create_task(get_movie_info(page)) for page in range(10)]
    # asyncio.run(asyncio.wait(tasks))

    asyncio.run(main())
